Home | | Latest | About | Random
# Week 2 Monday.
Dear Class,
Some announcements. (1) HW1 due this Thursday 10 pm. (2) If you have a class with a final exam that conflicts with this class's final exam, read the announcement on BruinLearn and fill out the form in that announcement.
Today and tomorrow:
- Fundamental types
- `bool`
- `char`
- `unsigned int`
- `size_t` (of ``)
- Class
- `string` class
- using member functions of a class
This week's goal: Finish section 5 (variables and fundamental types), 6 (`string` class), and 7 (input buffer).
-- B.
## Some mysterious calculations.
Look at the following code and try to predict the outcome.
```cpp
#include
#include
using namespace std;
int main(){
cout << ('6' < 7) << endl;
cout << 'a' + 1 << endl;
string s("Hello");
cout << s << endl;
cout << s.length() << endl;
cout << s.length() - 10 << endl;
return 0;
}
```
Notice the single-quotes here for `'6'` and `'a'`. What's going on!
## `bool`. Yes or no?
- A `bool` stores `true` or `false`. And we get a `bool` by **asking a question**.
- `bool` is short for the word Boolean, named after George Boole who contributed to mathematical logic.
- Some operations that would yield a `bool`:
| Operator | (Meaning) | Operator | (Meaning) |
| -------- | ---------------- | -------- | ------------------------- |
| `==` | equal to? | `!=` | not equal to? |
| `<` | less than to? | `>=` | greater than or equal to? |
| `>` | greater than to? | `<=` | less than or equal to? |
```cpp
#include
using namespace std;
int main(){
bool is_five = (3 + 2 == 5);
cout << is_five << endl;
cout << boolalpha;
cout << is_five << endl;
return 0;
}
```
- `std::cout` defaults to printing `bool`s as `1` or `0`, but if we feed `std::cout` with the manipulator `std::boolalpha`, then `bool`s are now printed as `true` or `false`.
**Predict.**
Which is bigger $99^{101}$ or $100^{100}$? Is `0.1 + 0.1 + 0.1` the same as `0.3`?
```cpp
#include
#include
using namespace std;
int main(){
double a = 0.1;
double c = 0.3;
cout << a + a + a << endl;
cout << boolalpha
<< (a + a + a == c) << endl;
cout << ( pow(99,101) < pow(100, 100));
return 0;
}
```
- Comparing `double`s with `==` is always risky. Also when `double`s get really huge we lose precision and can only trust so many significant figures. By reading cppreference, we will find that `pow` returns a `double` (or some decimal number).
- Comparing `int` with `==` is always safe, so long as we are within the limits of `int` (which is about minus 2 billion to 2 billion.)
**Checking even trick.**
If `int i` is assigned some integer. What can we do to check if it is an even integer? Write one line to show `true` if `i` is even, and show `false` if `i` is odd.
What if we want to show `true` if `i` is odd, and show `false` if `i` is even?
**Pitfall! `=` is not `==`.**
Recall `=` is the assignment operator, while `==` is the equality comparison operator. A typo often results in bugs!
```cpp
#include
using namespace std;
int main(){
int x = 5;
cout << (x == 10) << endl;
cout << (x = 10) << endl;
cout << (x == 10) << endl;
return 0;
}
```
**Implicit casting.**
If you assign any number to a `bool`, then zero will be assigned to be `false`, and everything else will be assigned to be `true`.
```cpp
#include
using namespace std;
int main(){
bool b = 0.5;
cout << b ;
return 0;
}
```
## `char` is secretly a "small number".
- `"a"` is a string literal, this one in particular is a `const char[2]` (we will return to this much later). Notice the double-quotes.
- `'a'` is a `char`, notice the single-quotes enclosing a single character.
- Under the hood, a `char` is a small integer called its **codepoint**, which is given by the ASCII table in the [textbook](https://www.pic.ucla.edu/~mjandr/resources/10A-textbook/5.html#section-5-10) and most places online.
- `'A'` is `65`
- `'a'` is `97`
- `'0'` is `48`
- And all you need to know really is that the letters and numbers are all in order sequentially. So `'b'` would be `98` while `C` will be `67`.
**Casting.**
When assigning an `int` to a `char` variable, if it is a codepoint of a character as given in the ASCII table above, then we will get that character. When doing arithmetic between `char` and `int`, the `char` will be casted as `int`.
It is still best to cast explicitly, using `static_cast(ch)` for a `char ch` to its codepoint in `int`; and using `static_cast(n)` for an `int n` to the `char` corresponding to that codepoint.
```cpp
#include
using namespace std;
int main(){
char ch = 'a';
cout << ch << ' ' << ch + 1 << endl;
cout << static_cast(ch) << endl;
cout << static_cast(ch + 1) << endl;
return 0;
}
```
**Some practice.**
Using arithmetic on `char` can yield interesting effects. Try the following
- Turn the `char` `'6'` to the `int` `6`.
- Turn the `char` `'r'` to the `char` `'R'`.
- Turn the `char` `'R'` to the `char` `'r'`.
## A casting cheat sheet so far.
We do `static_cast(x)` to attempt to cast `x` as into type `T`.
| from | to | effect |
| ---------- | -------- | -------------------------------------------------------- |
| `double` | `int` | truncates the fractional parts |
| `int` | `double` | gains `.0` |
| `char` | `int` | get codepoint |
| `int` | `char` | get `char` with that codepoint |
| any number | `bool` | get `false` if `0`, else get `true` for any other number |
| `bool` | `int` | get `1` for `true`, and get `0` for `false` |
**Implicit casting.** Can occur when you mixed types. It is best to be explicit in your casting to show intent usually.
Now, check back to the mystery above!
---
**We ended here. But keep reading ahead. Finish reading section 5, start reading section 6.**